// pages/user/erji//addOrEditAddress/addOrEditAddress.js
var base = require('../../../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const PAGES = require('../../../../utils/pages.js')
import AreaList from '../../../../lib/area/area.js';
import AreaListEn from '../../../../lib/area/area-en';
const Log = require('../../../../lib/log.js')

const { getUserToken, getUserInfo } = require('../../../../lib/user.js')
const { URL } = require('../../../../lib/urls.js')
const { HTTP } = require('../../../../lib/http.js')
const app = getApp();
var amapFile = require('../../../../lib/amap-wx.js');//如：..­/..­/libs/amap-wx.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    address:"请设置",
    latitude: '',
    longitude: '',
    showSSQ:false,
    areaList:null,
    detailAddress:"",
    proviceCode:"",
    cityCode: "",
    areaCode: "",
    provice: "",
    city: "",
    area: "",
    name: "",
    mobile: "",
    addressCode:"",
    checked:false,
    addressLoc:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      _t: base._t(), //翻译
    });
    var item = JSON.parse(decodeURIComponent(options.data));
    this.setData({
      addressCode: item.AddressCode,
      detailAddress: item.DetailAddress,
      proviceCode: item.ProviceCode,
      cityCode: item.CityCode,
      areaCode: item.AreaCode,
      provice: item.Provice,
      city: item.City,
      area: item.Area,
      name: item.Name,
      mobile: item.Mobile,
      address: item.Provice + " " + item.City + " " + item.Area + " " + item.DetailAddress,
      checked: item.IsDefault==1?true:false,
      latitude: item.Latitude,
      longitude: item.Longitude,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var lang = wx.getStorageSync('Language') || 'zh_CN';
    this.setData({
      areaList: lang == 'en' ? AreaListEn : AreaList,
    })
  },

  // 内容编辑
  bindDetalAddTag: function (e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  bindSHRTag: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindTelTag: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  /**
   * 选择地址
   */
  selectAddressTap: function () {
    this.setData({
      showSSQ:true,
    })
  },
  /**
   * 选择地图地址
   */
  selectAddressLocTap: function () {
    var myAmapFun = new amapFile.AMapWX({ key: 'c40a27d8e31f407e574b6caa1aa06518' });
    app.getPermission();
    var _this=this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(res)

      }
    })
    wx.chooseLocation({
      success(res) {
        console.log(res)
        if (res.address !== '' && res.name !== '') {
          　　　　　　　　　　　　//确认选择地址之后adress和name才不会为空
          _this.setData({
            latitude:res.latitude,
            longitude:res.longitude,
            addressLoc: res.address + res.name,
          })
        }
      }
    })
  },
  // 弹出框控制
  onClose() {
    this.setData({
      showSSQ: false,
    });
  },
  // 省市区选择确认
  onSsqConfirm(event) {
    const { values, indexs } = event.detail;
    var ssq="";
    for (var i = 0; i < values.length;i++){
      ssq+=(values[i].name+' ');
    }
    this.setData({
      address:ssq,
      proviceCode:values[0].code,
      cityCode: values[1].code,
      areaCode: values[2].code,
      provice: values[0].name,
      city: values[1].name,
      area: values[2].name,
    });
    this.onClose();
  },

  //保存地址
  saveAddress: function () {
    
    if (!this.data.area||this.data.area.length<1){
      wx.showToast({
        title: '请选择省市区',
        icon: 'none'
      })
      return;
    }
    if (this.data.name.length < 1) {
      wx.showToast({
        title: '请设置收件人',
        icon: 'none'
      })
      return;
    }
    if (this.data.mobile.length < 1) {
      wx.showToast({
        title: '请设置联系电话',
        icon: 'none'
      })
      return;
    }
    if (!this.data.latitude || this.data.area.latitude < 1) {
      wx.showToast({
        title: '请选择地图地址',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '正在处理...'
    })
    HTTP({
      url: URL.address.addOrChang,
      userToken: getUserToken(),
      data: {
        AddressCode:this.data.addressCode,
        Name: this.data.name,
        Mobile: this.data.mobile,
        Provice: this.data.provice,
        City: this.data.city,
        Area: this.data.area,
        DetailAddress: this.data.detailAddress,
        ProviceCode: this.data.proviceCode,
        CityCode: this.data.cityCode,
        AreaCode: this.data.areaCode,
        MemberId: getUserInfo().memberId,
        IsDefault:this.data.checked?1:0,
        Latitude: this.data.latitude,
        Longitude: this.data.longitude,
      },
      success: (res) => {
        wx.showToast({
          title: '地址保存成功',
        })
        var pages = getCurrentPages();//当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
          beforePage.getNetAddressList();//触发父页面中的方法
        }
        setTimeout(function () {
          //你需要执行的代码
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  // 删除地址
  delTap: function () {
    //获取会员信息-会员到期时间，公司名称
    HTTP({
      url: URL.address.deleteAdd,
      userToken: getUserToken(),
      data: {
        AddressCode: this.data.addressCode,
        Name: this.data.name,
        Mobile: this.data.mobile,
        Provice: this.data.provice,
        City: this.data.city,
        Area: this.data.area,
        DetailAddress: this.data.detailAddress,
        ProviceCode: this.data.proviceCode,
        CityCode: this.data.cityCode,
        AreaCode: this.data.areaCode,
        MemberId: getUserInfo().memberId,
      },
      success: (res) => {
        wx.showToast({
          title: '删除成功',
        })
        var pages = getCurrentPages();//当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
          beforePage.getNetAddressList();//触发父页面中的方法
        }
        setTimeout(function () {
          //你需要执行的代码
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  }
})